package com.tmc.dao.impl.intair;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.constant.TaskConstant.TaskStatus;
import com.tmc.dao.impl.BaseDaoImpl;
import com.tmc.entity.intair.IntairChaseCabin;
import com.tmc.util.ObjectUtil;

@Repository
public class IntairChaseCabinDaoImpl extends BaseDaoImpl<IntairChaseCabin> {

	public Pagination<IntairChaseCabin> findByCon(ContextInfo context, IntairChaseCabin cond, int pageIndex,
			int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from IntairChaseCabin c where c.enabledFlag = 'Y' ");
		if (ObjectUtil.hasText(cond.getPnr())) {
			sql.append(" and c.pnr = :pnr ");
			params.put("pnr", cond.getPnr());
		}
		sql.append(" order by c.lastUpdateDate desc ");
		return this.findPagination(sql.toString(), params, pageIndex, pageSize);
	}
	
	public Pagination<IntairChaseCabin> findCurrent(ContextInfo context, IntairChaseCabin cond, int pageIndex,
			int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from IntairChaseCabin c where c.enabledFlag = 'Y' ");
		if (ObjectUtil.hasText(cond.getPnr())) {
			sql.append(" and c.pnr = :pnr ");
			params.put("pnr", cond.getPnr());
		}
		sql.append(" and c.status = 'WAITING'");
		sql.append(" order by c.lastUpdateDate desc ");
		return this.findPagination(sql.toString(), params, pageIndex, pageSize);
	}
	
	public Pagination<IntairChaseCabin> findHistory(ContextInfo context, IntairChaseCabin cond, int pageIndex,
			int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from IntairChaseCabin c where c.enabledFlag = 'Y' ");
		if (ObjectUtil.hasText(cond.getPnr())) {
			sql.append(" and c.pnr = :pnr ");
			params.put("pnr", cond.getPnr());
		}
		sql.append(" and c.status <> 'WAITING'");
		sql.append(" order by c.lastUpdateDate desc ");
		return this.findPagination(sql.toString(), params, pageIndex, pageSize);
	}

	public List<IntairChaseCabin> findByPNR(ContextInfo context, String pnr) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from IntairChaseCabin c where c.enabledFlag = 'Y' ");
		if (ObjectUtil.hasText(pnr)) {
			sql.append(" and c.pnr = :pnr ");
			params.put("pnr", pnr);
		}
		sql.append(" order by c.lastUpdateDate desc ");
		return this.findList(sql.toString(), params);
	}

	public List<IntairChaseCabin> findWait(ContextInfo context, int max) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from IntairChaseCabin c where c.enabledFlag = 'Y' ");
		sql.append(" and c.status = :status ");
		params.put("status", TaskStatus.WAITING.getStatus());
		sql.append(" order by c.lastUpdateDate ");
		return this.findList(max, sql.toString(), params);
	}
}
